package com.situ.science.controller;

import com.situ.science.pojo.Admin;
import com.situ.science.pojo.Researchers;
import com.situ.science.pojo.dto.PasswordDto;
import com.situ.science.service.IAdminService;
import com.situ.science.service.IResearchersService;
import com.situ.science.utils.CommonUtil;
import com.situ.science.utils.JwtUtil;
import com.situ.science.utils.Result;
import com.situ.science.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService adminService;
    @Autowired
    RedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin, HttpServletRequest request) {
        System.out.println("AdminController.login");

        //写查询语句,不对,必须得写token,不然就得用session
        String captcha = (String) redisTemplate.opsForValue().get(CommonUtil.getCaptchaKey(request));
        if (StringUtils.isEmpty(captcha)) {
            return Result.error("验证码已失效");
        }
        if (!captcha.equalsIgnoreCase(admin.getCaptcha())) {
            return Result.error("请输入正确验证码");
        }
        System.out.println("这里执行了吗");
        Admin logeinAdmin = adminService.login(admin);
        System.out.println("logeinAdmin = " + logeinAdmin);
       if (logeinAdmin == null || logeinAdmin.getPassword() == null) {
            return Result.error("wrong username or password");
        }
        //successful login
        //已知要调用一个map，执行JwtUtil.createToken(map);
        //并且map中存放的是用户名和密码
        Map<String, Object> map = new HashMap<>();

            map.put("id", logeinAdmin.getId());
            map.put("name", logeinAdmin.getName());


        String token = JwtUtil.createToken(map);
        System.out.println("token: " + token);
        //现在前台点击login会返回一个状态码code和一个内容token的data
        return Result.ok("successful login",token);
    }

    @RequestMapping("/info")
    public Result info(@RequestHeader(name = "Authorization") String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer id = (Integer) map.get("id");
        Admin admin = adminService.info(id);
        return Result.ok(admin);
    }
    @RequestMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return Result.ok();
    }

    @RequestMapping("/resetPassword")
    public Result resetPassword(@RequestBody PasswordDto passwordDto) {
        Map<String,Object> map = ThreadLocalUtil.get();
        System.out.println(map.get("id"));
        Integer id = (Integer) map.get("id");
        //1.判断旧密码是否正确
        String password = adminService.checkPassword(passwordDto.getOldPassword(),id);
        if (password == null) {
            return Result.error("旧密码错误");
        }
        //2.根据id修改密码
        adminService.resetPassword(passwordDto,id);
        return Result.ok();
    }
}
